
package com.nit.lab.dao.Impl;

import com.nit.lab.dao.HisOrderDao;
import com.nit.lab.dto.DtoOrderFile;
import com.nit.lab.dto.DtohisOrder;

import org.hibernate.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by CYT on 2017/12/5.
 */

@Repository
public class HisOrderDaolmpl implements HisOrderDao {

    @Resource
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public Integer getHisOrderDataTableCount(String search, Integer pre, Integer actorId) throws Exception {
        Integer result = 0;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        if (actorId == 3 || actorId == 2) {
            hql = "select count(*) from BaseOrderEntity o, SysStoreEntity s " +
                    "where o.orderStatus = 5 and s.storeId = o.storeId and o.userId =:pre and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
            query.setInteger("pre", pre);
        } else {
            hql = "select count(*) from BaseOrderEntity o, SysStoreEntity s, SysUserEntity sue " +
                    "where o.orderStatus = 5 and s.storeId = o.storeId and sue.userId = o.userId and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
        }
        result = Integer.parseInt(query.uniqueResult().toString());
        return result;
    }

    @Override
    public List<DtohisOrder> getHisOrderDataTable(Integer start, Integer length, String search, Integer pre, Integer actorId) throws Exception {
        List<DtohisOrder> result = null;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        if (actorId == 3 || actorId == 2) {
        /*用户,商家，只能看自己的历史订单*/
            hql = "select new com.nit.lab.dto.DtohisOrder(o.orderFiles,o.orderId, o.orderTime,s.storeId, s.storeName, " +
                    "s.storeAddress, s.storeEvaluate, o.orderPay, o.orderStatus, o.orderEvaluate) " +
                    "from BaseOrderEntity o, SysStoreEntity s" +
                    " where o.orderStatus = 5 and s.storeId = o.storeId and o.userId =:pre and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
            query.setInteger("pre", pre);
        } else {
            hql = "select new com.nit.lab.dto.DtohisOrder(o.orderFiles,o.orderId, o.orderTime,s.storeId, s.storeName, " +
                    "s.storeAddress, s.storeEvaluate, o.orderPay, o.orderStatus, o.orderEvaluate) " +
                    "from BaseOrderEntity o, SysStoreEntity s , SysUserEntity sue " +
                    " where o.orderStatus = 5 and s.storeId = o.storeId and sue.userId = o.userId and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
        }
        result = query.setFirstResult(start).setMaxResults(length).list();
        return result;
    }

    @Override
    public DtoOrderFile getFileInfoByOrderId(Integer orderFileId) throws Exception {
        DtoOrderFile dtoOrderFile = null;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        hql = "select new com.nit.lab.dto.DtoOrderFile(afe.fileName,bfe.orderFileId, bfe.fileId, bfe.paperSizeId, bfe.paperColorId, bfe.paperTypeId, bfe.printCount," +
                " bfe.filePrice) from BaseOrderFileEntity bfe,SysFileEntity afe" +
                " where orderFileId =:orderFileId and bfe.fileId = afe.fileId";
        query = session.createQuery(hql);
        query.setInteger("orderFileId", orderFileId);
        dtoOrderFile = (DtoOrderFile) query.uniqueResult();
        return dtoOrderFile;
    }

    @Override
    public Integer insertEvaluate(Integer orderId, Integer evaluate) throws Exception {
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query = null;
        hql = "update BaseOrderEntity set orderEvaluate =:evaluate where orderId =:orderId";
        query = session.createQuery(hql);
        query.setInteger("evaluate", evaluate);
        query.setInteger("orderId", orderId);
        return query.executeUpdate();
    }

}

